/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.controller;

import edu.soc.cs5226.gui.entities.*;
import edu.soc.cs5226.gui.entities.util.TimeUtil;
import edu.soc.cs5226.gui.entities.util.Constants;
import java.io.Serializable;  
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;
  
import org.primefaces.model.DefaultTreeNode;  
import org.primefaces.model.TreeNode;  
  
/**
 * 
 *
 * @author Huijia
 */
  
@ManagedBean
@SessionScoped
public class SharedPoolController implements Serializable {
    private DAO openDAO;
    private TreeNode root;
    private InitParameter spParamObj;
    private static final String SHAREDPOOL_PAGE = "sharedpool.xhtml";
        
    public SharedPoolController() 
    {  
        openDAO = new DAO();
        obtainInitParameter();
    }
	
    public TreeNode getRoot() 
    {
            return root;
    }

    public InitParameter getSpParamObj() {
        return spParamObj;
    }

    public void setSpParamObj(InitParameter spParamObj) {
        this.spParamObj = spParamObj;
    }
    
    public void obtainInitParameter() {
        try{
            setSpParamObj(openDAO.getInitParam(Constants.SHAREDPOOLINIT));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String processViewMore() 
    {
        try
        {
            root = new DefaultTreeNode("root",null);
            Timestamp now = new Timestamp(TimeUtil.getInstance().getTime());
            List<DBParameter> sharedpoolList = openDAO.getspsList(now);
            if (!sharedpoolList.isEmpty())
            {
                HealthStatus thresholds = openDAO.getSPSHealthStatus();

                CalculateXY calcXY = new CalculateXY();
                List<xObject> xList = calcXY.process(sharedpoolList, thresholds, Constants.SHAREDPOOL);
                dayObject dayObj = new SharedPoolProcessList().processTreeNode(now, xList, thresholds);

                TreeNode treenode24 = new DefaultTreeNode((SharedPool)dayObj.getRootObj(),root);

                Iterator<xObject> xIter = dayObj.getxObjects().iterator();
                while (xIter.hasNext())
                {
                    xObject currX = xIter.next();
                    SharedPool spObjX = (SharedPool)currX.getxObj();

                    TreeNode treeNodeX = new DefaultTreeNode(spObjX,treenode24);

                    Iterator<yObject> yIter = currX.getyObjects().iterator();
                    while (yIter.hasNext())
                    {
                        yObject currY = yIter.next();
                        SharedPool spObjY = (SharedPool)currY.getyObj();

                        TreeNode treeNodeY = new DefaultTreeNode(spObjY,treeNodeX);

                        Iterator<DBParameter> spIter = currY.getObj().iterator();
                        while (spIter.hasNext())
                        {
                            SharedPool sp = (SharedPool)spIter.next();

                            TreeNode treeNodeSP = new DefaultTreeNode(sp,treeNodeY);
                        }
                    }
                }
            } // if (!sharedpoolList.isEmpty())
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return SHAREDPOOL_PAGE;
    } // processViewMore
}  